﻿@using Himall.Core;
@model Himall.Model.OrderInfo

<script src="~/Scripts/jquery.himallLinkage.nocreate.js"></script>
<script src="~/Scripts/Region.js"></script>
<script src="~/Scripts/RegionBind.js"></script>
<div class="container">
    <ul class="nav nav-tabs-custom clearfix">
        <strong>订单</strong>
        <li><a href="/selleradmin/order/Management">所有订单</a></li>
        @if (!string.IsNullOrEmpty(Request.QueryString["updatePrice"]))
        {
            <li class="active"><a>改价</a></li>
        }
        else
        {
            <li class="active"><a>查看</a></li>
        }
    </ul>

    <h3 class="table-hd">商品清单</h3>
    <table id="tbOrderItems" class="table table-bordered">
        <thead>
            <tr>
                <th>商品</th>
                <th>数量</th>
                <th>单价</th>
                <th>改价</th>
                <th>总价</th>
                @*<th>运费</th>*@
            </tr>
        </thead>
        <tbody>
            @{
                var service = Himall.Web.Framework.ServiceHelper.Create<Himall.IServices.IProductService>();
                var regionService = Himall.Web.Framework.ServiceHelper.Create<Himall.IServices.IRegionService>();
                var OrderItems = Model.OrderItemInfo.Select(r =>
                {
                    var product = service.GetProduct(r.ProductId);
                    return new
                    {
                        Id = r.Id,
                        FreightTemplateId = product != null ? product.FreightTemplateId : 0,
                        SalePrice = r.SalePrice,
                        Quantity = r.Quantity,
                        ProductId = r.ProductId,
                        ProductName = r.ProductName,
                        Color = r.Color,
                        Size = r.Size,
                        Version = r.Version,
                        SkuId = r.SkuId,
                        DiscountAmount = r.DiscountAmount,
                        ThumbnailsUrl = r.ThumbnailsUrl
                    };
                }
               );
                var freightProductGroup = OrderItems.GroupBy(item => item.FreightTemplateId);
            }
            @foreach (var items in freightProductGroup)
            {
                var products = items.GroupBy(r => r.ProductId);
                IEnumerable<long> productIds = products.Select(r => r.Key);
                IEnumerable<int> counts = products.Select(r => r.Sum(p => (int)p.Quantity));

                string RegionIdPath = regionService.GetRegionIdPath(Model.RegionId);
                int cityId = regionService.GetCityId(RegionIdPath);
                decimal freight = freight = service.GetFreight(productIds, counts, cityId);

                int rows = 0;
                foreach (var orderItem in items)
                {
                    rows++;
                    <tr class="product">
                        <td>
                            <a target="_blank" href="/product/detail/@orderItem.ProductId">
                                <img width="50" height="50" title="" src="@orderItem.ThumbnailsUrl">@orderItem.ProductName @orderItem.Color @orderItem.Size @orderItem.Version

                            </a>

                        </td>
                        <td>@orderItem.Quantity</td>
                        <td class="ftx-04">@orderItem.SalePrice.ToString("F2")</td>
                        <td class="ftx-04">
                            @if (Model.OrderStatus == Himall.Model.OrderInfo.OrderOperateStatus.WaitPay && (bool)ViewBag.UpdatePrice == true)
                            {
                                <select id="updateWay">
                                    <option value="1">减</option>
                                    <option value="2">增</option>
                                </select>
                                <input id="inputDiscount@(orderItem.Id)" onkeyup="(this.v = function () { this.value = this.value.replace(/[^0-9]+/, ''); }).call(this)" onblur="    this.v()" class="text-order no-m" type="text">
                                <span class=" btn btn-warning btn-xs ss" itemid="@orderItem.Id">确认改价</span>
                            }
                            else
                            {
                                if (orderItem.DiscountAmount > 0)
                                {
                                    <span>减: @orderItem.DiscountAmount.ToString("F2")</span>
                                }
                                else
                                {
                                    <span>增: @Math.Abs(orderItem.DiscountAmount).ToString("F2")</span>
                                }

                            }
                        </td>
                        <td class="ftx-04">@(orderItem.SalePrice * orderItem.Quantity - orderItem.DiscountAmount)</td>

                        @*@if (rows == 1)
                            {
                                <td rowspan="@items.Count()">
                                    @(freight.ToString("F2"))
                                </td>
                            }*@
                    </tr>
                }
            }
            <tr>
                <td align="right" colspan="6">
                    <b>商品总价： <span class="ftx-04">￥@Model.ProductTotalAmount.ToString("F2") </span></b>
                    运费：
                    @if (Model.OrderStatus == Himall.Model.OrderInfo.OrderOperateStatus.WaitPay && (bool)ViewBag.UpdatePrice == true)
                    {
                        <input class="ftx-04" type="text" class="text-order no-m" id="freight" value="@Model.Freight.ToString("F2")" />
                        <span class=" btn btn-warning btn-xs" id="updateFreight" itemid="@Model.Id">确认改价</span>

                    }
                    else
                    {
                        <span class="ftx-04"> @Model.Freight.ToString("F2")</span>
                    }

                    @*<b >*@
                    @*改价：
                            @{var itemDis = Model.OrderItemInfo.Sum(i => i.DiscountAmount);}
                            @if (itemDis > 0)
                            {
                                <b>减 <span class="ftx-04">@itemDis</span></b>
                            }
                            else
                            {
                                <b>增 <span class="ftx-04">@(-itemDis)</span></b>
                            }
                        </b>*@
                    @if (Model.DiscountAmount > 0)
                    {
                        <label> 优惠券抵扣金额:￥@Model.DiscountAmount.ToString("F2");</label>
                    }
                    @if (Model.IntegralDiscount > 0)
                    {
                        <label> 积分抵扣金额:￥@Model.IntegralDiscount.ToString("F2");</label>
                    }
                    <b>实付金额：<span class="ftx-04">￥@Model.OrderTotalAmount.ToString("F2")</span></b>

                </td>

            </tr>
        </tbody>
    </table>


    <h3 class="table-hd">
        订单信息
        @if (Model.OrderStatus == Himall.Model.OrderInfo.OrderOperateStatus.WaitPay || Model.OrderStatus == Himall.Model.OrderInfo.OrderOperateStatus.WaitDelivery)
        {
            <a class="order-info-resive" href="javascript:void(0)"> 修改</a>
        }
    </h3>
    <div class="order-info">
        <div class="caption">
            <p>&nbsp;订 单 号：@Model.Id</p>
            <p>下单时间：@Model.OrderDate.ToString("yyyy-MM-dd HH:mm:ss")</p>
            <p>
                订单状态：@Model.OrderStatus.ToDescription()
                @if ((int)Model.OrderStatus == 4)
                {
                    @Model.CloseReason
                }
            </p>
            <p>下单会员：@Model.UserName</p>
        </div>
        <div class="caption">
            <p>收 货 人：<span id="spShipTo">@Model.ShipTo</span></p>
            <p>地    址：@Model.RegionFullName <span id="spAddress">@Model.Address</span></p>
            <input type="hidden" id="hidRegionIdPath" />
            <input type="hidden" id="hidTopRegionId" value="@Model.TopRegionId" />
            <input type="hidden" id="hidRegionId" value="@Model.RegionId" />
            <input type="hidden" id="hidCityId" value="0" />
            <p>手机号码：<span id="spCellPhone">@Model.CellPhone</span></p>
            @*<p>买家备注：@Model.UserRemark</p>*@
        </div>
        <div class="caption">
            @if (Model.PayDate != null && Model.OrderStatus != Himall.Model.OrderInfo.OrderOperateStatus.WaitPay)
            {
                if (@Model.OrderTotalAmount == 0)
                {
                    <p>支付方式： <span>积分支付</span></p>
                }
                else if (string.IsNullOrEmpty(Model.PaymentTypeName))
                {
                    <p>支付方式：平台确认收款  【备注：@{@Model.PayRemark}】</p>
                }
                else
                {

                    <p>支付方式：  @Model.PaymentTypeName @Model.PayRemark</p>
                }
            }
            @if (!string.IsNullOrEmpty(Model.ExpressCompanyName))
            {
                <p>物流公司：@Model.ShowExpressCompanyName</p>
            }
            @if (!string.IsNullOrEmpty(Model.ShipOrderNumber))
            {
                <p>快递单号：@Model.ShipOrderNumber</p>
            }
        </div>
        @if (Model.InvoiceType != Himall.Model.InvoiceType.None)
        {
            <div class="caption">
                <p>发票抬头：@Model.InvoiceTitle</p>
                <p>发票内容：@Model.InvoiceContext</p>
            </div>
        }
        else
        {
            <div class="caption">
                <p>不需要开发票</p>
            </div>
        }

    </div>

    <h3 class="table-hd">物流跟踪</h3>
    <table class="table table-bordered" id="tbExpressData"></table>

    <h3 class="table-hd">操作历史</h3>
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>操作者</th>
                <th>时间</th>
                <th>描述</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var orderOperationLog in Model.OrderOperationLogInfo)
            {
                <tr>
                    <td>@orderOperationLog.Operator</td>
                    <td>@orderOperationLog.OperateDate</td>
                    <td>@orderOperationLog.OperateContent</td>
                </tr>
            }
        </tbody>
    </table>


</div>
<script type="text/javascript">
    var isPosting=false;
    $(function () {

        var expressCompanyName="@Model.ExpressCompanyName";
        var shipOrderNumber=  "@Model.ShipOrderNumber" ;

        if(expressCompanyName!=""||shipOrderNumber!="")
        {
            $.post('../GetExpressData', { expressCompanyName:expressCompanyName, shipOrderNumber:shipOrderNumber}, function (result) {
                var html;
                var obj = jQuery.parseJSON(result);
                if (obj.message == "ok") {
                    var data = obj.data;
                    for (var i = 0; i < data.length; i++) {
                        html += '<tr><td>' + data[i].time + '</td>\
                             <td>' + data[i].context + '</td>';
                        html += '</tr>';
                    }
                }

                else {
                    html += '<tr><td>该单号暂无物流进展，请稍后再试，或检查公司和单号是否有误。</td></tr>';
                }

                html += '<tr><td><a href="http://www.kuaidi100.com" target="_blank" id="power" runat="server" style="color:Red;">此物流信息由快递100提供</a></td></tr>';

                $("#tbExpressData").html(html);
            });
        }

        // 物流信息


        $('.btn-xs.ss').click(function () {
            //$(this).fadeOut(200);
            var itemid = $(this).attr("itemid");
            var discountAmount = $("#inputDiscount" + itemid).val();
            if ($("#updateWay").val() == 2)
                discountAmount = -discountAmount;
            if(isNaN(discountAmount)||discountAmount.length==0)
            {
                $.dialog.errorTips("请输入正确金额！");
                return false;
            }
            if ($("#updateWay").val() == 1 && isNaN($(this).parent().next().html() < discountAmount))
                $.dialog.errorTips("减价金额不能大于实付金额！");
            var url='@Url.Action("UpdateItemDiscountAmount")';
            var loading = showLoading();
            if(isPosting) return;
            isPosting=true;
            $.post(url, { orderItemId: itemid, discountAmount: discountAmount }, function (result) {
                loading.close();
                isPosting=false;
                if (result.success) {
                    $.dialog.succeedTips("改价成功！",function(){location.href = location.href;});
                    // location.href = "./@Model.Id/?updatePrice="+true;

                }
                else
                    $.dialog.errorTips("操作失败"+result.msg);
            });
        });

        $("#updateFreight").click(function () {
            var frieght = $("#freight").val();
            if(frieght<0)
                $.dialog.errorTips("运费金额不能为负！");
            var loading = showLoading();
            $.post('@Url.Action("UpdateOrderFrieght")', { orderId: @Model.Id, frieght: frieght }, function (result) {
                loading.close();
                if (result.success) {
                    $.dialog.succeedTips("改价成功！",function(){location.href = location.href;});

                }
                else
                    $.dialog.errorTips("操作失败" + result.msg);
            });
        });
        $.post('@Url.Action("GetRegionIdPath")', { regionId: "@Model.RegionId" }, function (result) {
            $("#hidRegionIdPath").val(result);
        });

        // 修改收货地址
        $('.order-info-resive').click(function () {
            $.dialog({
                title: '修改收货地址',
                lock: true,
                id: 'orderInfoResive',
                content: ['<div class="dialog-form">',
                    '<div class="form-group">',
                        '<label class="label-inline" for="">收货人</label>',
                        '<input class="form-control input-sm" type="text" id="txtShopTo" value="' + $("#spShipTo").html() + '">',
                    '</div>',
                    '<div class="form-group" id="regionAddr">',
                        '<label class="label-inline" for="">地址</label>',
                        '<select id="region1" class="form-control input-sm select-sort"><option></option></select>',
                        '<select id="region2" class="form-control input-sm select-sort"><option></option></select>',
                        '<select id="region3" class="form-control input-sm select-sort"><option></option></select>',
                        '<br/>',
                        '<input class="form-control input-sm input-only-line" type="text" id="txtAddress" value="' + $("#spAddress").html() + '">',
                    '</div>',
                    '<div class="form-group">',
                        '<label class="label-inline" for="">手机号码</label>',
                        '<input class="form-control input-sm" type="text" id="txtCellPhone" value="' + $("#spCellPhone").html() + '">',
                    '</div>',

                '</div>'].join(''),
                padding: '20px 10px',
                button: [
               {
                   name: '确认修改',
                   callback: function () {
                       if (isSelectAddr($('#region1'),$('#region2'),$('#region3'))){
                           UpdateAddress();
                       }
                       else{
                           $('#regionAddr').css({ border: '1px solid #f60' });
                           return false
                       }
                   },
                   focus: true
               }]
            });
            setProvince($('#region1'), $('#region2'), $('#region3'));
            InitRegion($('#region1'), $('#region2'), $('#region3'),$('#hidRegionIdPath').val());
            /*
            $('#region1,#region2,#region3').himallLinkage({
                url: '../getRegion',
                enableDefaultItem: true,
                defaultItemsText: '全部',
                defaultSelectedValues: $("#hidRegionIdPath").val().split(','),
                onChange: function (level, value, text) {
                    if (level == 0)
                    {$("#hidTopRegionId").val(value);
                        $('#hidCityId').val('0');
                        $("#hidRegionId").val('0');
                    }
                    if (level==1)
                    {
                        $('#hidCityId').val(value);
                        $("#hidRegionId").val('0');
                    }
                    if (level == 2)
                        $("#hidRegionId").val(value);
                }
            });*/
        });
    });

    function UpdateAddress() {
        var loading = showLoading();
        $.post('../UpdateAddress', {
            orderId: "@Model.Id", shipTo: $("#txtShopTo").val(), cellPhone: $("#txtCellPhone").val(),
            topRegionId: $("#hidTopRegionId").val(), regionId: $("#region3").val()=='0'?$('#region2').val():$("#region3").val(), address: $("#txtAddress").val()
        }, function (result) {
            loading.close();
            if (result.success) {
                $.dialog.succeedTips("操作成功！",function(){location.href = "../Detail/@Model.Id";});

            }
            else
                $.dialog.errorTips("操作失败");
        });
    }
</script>